/*
 * File Name：RelationMenu2.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： RelationMenu2.java
 * Modify By：res-jxliu
 * Modify Date：2015年3月21日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;


import com.ciwong.mobilelib.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

/**
 * 滑动显示菜单
 * 
 * @author res-jxliu
 * @version ciwong v.1.0 2015年3月21日
 * @since ciwong v.1.0
 */
public class RelationMenu2 extends RelativeLayout
{
    private static final int FIVE = 5;

    private final int scrollDuration = 500;

    private final int defaultTextSize = 14;

    private int curIndex;

    private String[] menus;

    private Drawable selectorDrawable;

    private int itemWidth;

    private int itemHeight;

    private Scroller scroller;

    private LinearLayout container;

    private View block;

    private LayoutParams params;

    private boolean isInit;

    private OnSelectChangedListener mOnSelectChangedListener;

    /**
     * 滑动显示菜单
     * 
     * @param context
     *            上下文
     * @param attrs
     *            设置属性
     */
    public RelationMenu2(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.relation_menu2);
        menus = a.getResources().getStringArray(
                a.getResourceId(R.styleable.relation_menu2_menus, -1));
        selectorDrawable = a
                .getDrawable(R.styleable.relation_menu2_selector_background);
        init(context);
    }

    /**
     * 滑动显示菜单
     * 
     * @param context
     *            上下文
     */
    public RelationMenu2(Context context)
    {
        super(context);
        init(context);
    }

    /**
     * 设置菜单选项变化事件
     * 
     * @param listener
     *            {@link OnSelectChangedListener}
     */
    public void setOnSelectChangedListener(OnSelectChangedListener listener)
    {
        mOnSelectChangedListener = listener;
    }

    /**
     * 设置按钮
     * 
     * @param ms
     *            菜单选项文字数组
     */
    public void setMenuArr(String[] ms)
    {
        this.menus = ms;
        int size = container.getChildCount();
        int tvIndex = 0;
        for (int i = 0; i < size; i++)
        {
            View view = container.getChildAt(i);

            if (view instanceof TextView)
            {
                ((TextView) view).setText(ms[tvIndex]);
                tvIndex++;
            }
        }
    }

    /**
     * 设置选中块背景
     * 
     * @param drawable
     *            选中块背景
     */
    public void setBlockGg(Drawable drawable)
    {
        selectorDrawable = drawable;
        block.setBackgroundDrawable(drawable);
    }

    /**
     * 获取当前项
     * 
     * @return 当前项
     */
    public int getCurItem()
    {
        return curIndex;
    }

    /**
     * 设置当前选中项
     * 
     * @param index
     *            当前选中项
     */
    public void setCurItem(int index)
    {
        if (index < menus.length)
        {
            selectItem(index);
        }
    }

    private void init(Context context)
    {
        scroller = new Scroller(context);
        block = getSequreBlock();
        addView(block);
        container = new LinearLayout(getContext());
        addView(container);
        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        container.setLayoutParams(lp);
        for (int i = 0; i < menus.length; i++)
        {
            TextView v = getTextView(i);
            container.addView(v);
            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                    0, LayoutParams.FILL_PARENT);
            lp1.leftMargin = FIVE;
            lp1.weight = 1.0f;
            lp1.gravity = Gravity.CENTER;
            v.setLayoutParams(lp1);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        if (menus != null && menus.length > 0 && !isInit)
        {
            isInit = true;
            itemWidth = (r - l) / menus.length;
            itemHeight = b - t;
            int size = getChildCount();
            for (int i = 0; i < size; i++)
            {
                View view = getChildAt(i);

                if (!(view instanceof TextView))
                {
                    view.layout(l + curIndex * itemWidth, t, l + (curIndex + 1)
                            * itemWidth, b);
                    params = new LayoutParams(itemWidth, itemHeight);
                    params.alignWithParent = true;
                    view.setLayoutParams(params);
                    break;
                }
            }
        }
    }

    /**
     * 
     * 获取文字控件
     * 
     * @param index
     * @return
     */
    private TextView getTextView(final int index)
    {
        TextView tv = new TextView(getContext());
        tv.setTextColor(Color.GRAY);
        // tv.setTextColor(Color.WHITE);
        tv.setTextSize(defaultTextSize);
        tv.setText(menus[index]);
        tv.setGravity(Gravity.CENTER);
        tv.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                selectItem(index);
            }
        });
        return tv;
    }

    private void selectItem(int index)
    {
        if (curIndex != index)
        {
            scroller.startScroll(scroller.getCurrX(), scroller.getCurrY(),
                    itemWidth * index - scroller.getCurrX(), 0, scrollDuration);
            curIndex = index;
            invalidate();
            if (mOnSelectChangedListener != null)
            {
                mOnSelectChangedListener.onSelectedChanged(index);
            }
        }
    }

    @Override
    public void computeScroll()
    {
        super.computeScroll();
        if (scroller.computeScrollOffset())
        {
            params.leftMargin = scroller.getCurrX();
            block.setLayoutParams(params);
            invalidate();
        }
    }

    /**
     * 获取滑块
     * 
     * @return
     */
    private View getSequreBlock()
    {
        View view = new View(getContext());
        view.setBackgroundDrawable(selectorDrawable);
        view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        return view;
    }

    // private class BlockView extends LinearLayout
    // {
    //
    // public BlockView(Context context)
    // {
    // super(context);
    // }
    //
    // @Override
    // public void computeScroll()
    // {
    // if (scroller.computeScrollOffset())
    // {
    //
    // scrollTo(scroller.getCurrX(), scroller.getCurrY());
    // invalidate();
    // // LayoutParams params = (LayoutParams) block.getLayoutParams();
    // System.out.println("x:" + scroller.getCurrX());
    // // params.leftMargin = scroller.getCurrX();
    // // block.setLayoutParams(params);
    // }
    // }
    // }

    /**
     * 
     * 选中切换监听器
     */
    public static interface OnSelectChangedListener
    {

        /**
         * 选择项发生变化
         * 
         * @param select
         *            {@link RelationMenu#SELECT_LEFT}<br />
         *            {@link RelationMenu#SELECT_RIGHT}
         */
        void onSelectedChanged(int select);
    }
}
